import { login, logout, getInfo, edit, findAdminById, findUserList } from '@/api/admin'
import { getAdminToken, setAdminToken, removeAdminToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getAdminToken(),
    name: '',
    avatar: '',
    id: '',
    adminName: '',
    adminPassword: '',
    phone: '',
    idCard: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ID: (adminInfo, id) => {
    adminInfo.id = id
  },
  SET_ADMINNAME: (adminInfo, adminName) => {
    adminInfo.adminName = adminName
  },
  SET_PASSWORD: (adminInfo, password) => {
    adminInfo.adminPassword = password
  },
  SET_PHONENUMBER: (adminInfo, phone) => {
    adminInfo.phone = phone
  },
  SET_IDCARD: (adminInfo, idCard) => {
    adminInfo.idCard = idCard
  },
  SET_REALNAME: (adminInfo, name) => {
    adminInfo.name = name
  }
}

const actions = {
  // admin login
  login({ commit }, adminInfo) {
    const { adminName, adminPassword } = adminInfo
    return new Promise((resolve, reject) => {
      login({ adminName: adminName, adminPassword: adminPassword }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setAdminToken(data.token)
        localStorage.setItem('admin_token', data.token)
        localStorage.setItem('adminId', data.admin.id)
        localStorage.setItem('admin', JSON.stringify(data.admin))
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // // del user
  // delUser({ commit }, id) {
  //   return new Promise((resolve, reject) => {
  //     delAdmin({ id: id }).then(res => {
  //       resolve(res.data)
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        if (!data.isTrue) {
          return reject('登录失效，请重新登录')
        }
        const { id, adminName, adminPassword, name, idCard, phone } = data.admin
        commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')
        commit('SET_REALNAME', name)
        commit('SET_ID', id)
        commit('SET_ADMINNAME', adminName)
        commit('SET_PASSWORD', adminPassword)
        commit('SET_IDCARD', idCard)
        commit('SET_PHONENUMBER', phone)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user edit
  edit({ commit, state }, userInfo) {
    const { username, password, email, phone, address } = userInfo
    const uid = localStorage.getItem('admin_id')
    return new Promise((resolve, reject) => {
      edit({ id: uid, username: username, password: password,
        email: email, phone: phone, address: address }).then(res => {
        commit('SET_ADMINNAME', res.data.admin.adminName)
        commit('SET_PASSWORD', res.data.admin.adminPassword)
        commit('SET_IDCARD', res.data.admin.idCard)
        commit('SET_PHONENUMBER', res.data.admin.phone)
        commit('SET_NAME', res.data.admin.name)
        resolve(res.data)
      }).catch(error => {
        reject(error.message)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeAdminToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user findUserList
  findUserList({ commit }, userInfo) {
    const { username, phone, pageNum } = userInfo
    console.log(userInfo)
    return new Promise((resolve, reject) => {
      findUserList({ username: username, phone: phone, pageIndex: pageNum }).then(res => {
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // admin findAdminById
  findAdminById({ commit }, id) {
    return new Promise((resolve, reject) => {
      findAdminById({ id: id }).then((res) => {
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeAdminToken() // must remove token first
      commit('RESET_STATE')
      resolve()
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

